package com.bac.rentmap.ad.flat.crawler;

import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.bac.rentmap.ad.crawler.CrawledAd;
import com.bac.rentmap.ad.flat.Constants;
import com.bac.rentmap.ad.flat.LocalAddress;
import com.bac.rentmap.ad.flat.LocalAddressPrefix;
import com.google.appengine.api.datastore.Text;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class FlatCrawledAd extends CrawledAd {

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private String city;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private String streetPrefix;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private String street;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private Integer building;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private String buildingPart;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private String room;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private String phone;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private Integer price;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private Boolean pricePerDay;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private Date publishingDate;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private String sourceUrl;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private String externalUrl;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private List<String> externalImages;

    @Persistent
    private Text description;

    /**
    * Sometimes crawled ads need information from different sources. This flat
    * can be used to delay transformation until all information crawled.
    */
    @Persistent
    private Boolean readyForTransformation;

    private LocalAddress localAddress;

    @Persistent
    private String secureId;

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
    private String email;

    public FlatCrawledAd() {

    }

    public String getCity() {
        return city;
    }

    public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setCity(String city) {
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public Integer getBuilding() {
        return building;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public Date getPublishingDate() {
        return publishingDate;
    }

    public void setPublishingDate(Date publishingDate) {
        this.publishingDate = publishingDate;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder(getClass().getSimpleName() + "@"
                + hashCode());
        res.append('[');
        res.append(street);
        res.append(',');
        res.append(building);
        res.append(',');
        res.append(getPhone());
        res.append(',');
        res.append(sourceUrl);
        res.append(']');
        return res.toString();
    }

    public String getRoom() {
        return room;
    }

    /**
    * @param room
    *            required
    */
    public void setRoom(String room) {
        if (Constants.ROOM_MARKER.equals(room)) {
            this.room = room;
        } else {
            if (room != null) {
                // Integer check
                Integer.valueOf(room);
            }
            this.room = room;
        }
    }

    public String getSourceUrl() {
        return sourceUrl;
    }

    public void setSourceUrl(String sourceUrl) {
        this.sourceUrl = sourceUrl;
    }

    public Boolean getReadyForTransformation() {
        return readyForTransformation;
    }

    public void setReadyForTransformation(Boolean readyForTransformation) {
        this.readyForTransformation = readyForTransformation;
    }

    public void setLocalAddress(LocalAddress localAddress) {
        this.localAddress = localAddress;
        LocalAddressPrefix localAddressPrefix = localAddress.getPrefix();
        if (localAddressPrefix != null)
            streetPrefix = localAddressPrefix.getName();
        street = localAddress.getStreet();
        building = localAddress.getBuilding();
        buildingPart = localAddress.getBuildingPart();
    }

    public LocalAddress getLocalAddress() {
        if (localAddress == null) {
            LocalAddressPrefix localAddressPrefix = null;
            if (streetPrefix != null) {
                localAddressPrefix = LocalAddressPrefix
                        .valueOfName(streetPrefix);
            }
            localAddress = new LocalAddress(localAddressPrefix, street,
                    building, buildingPart);
        }
        return localAddress;
    }

    public String getDescription() {
        if (description == null)
            return null;
        return description.getValue();
    }

    public void setDescription(String description) {
        this.description = new Text(description);
    }

    public String getSecureId() {
        return secureId;
    }

    public void setSecureId(String secureId) {
        this.secureId = secureId;
    }

    public Long getId() {
        return id;
    }

    public void setExternalUrl(String externalUrl) {
        this.externalUrl = externalUrl;
    }

    public String getExternalUrl() {
        return externalUrl;
    }

    public List<String> getExternalImages() {
        return externalImages;
    }

    public void setExternalImages(List<String> externalImages) {
        this.externalImages = externalImages;
    }

    public Boolean getPricePerDay() {
        return pricePerDay;
    }

    public void setPricePerDay(Boolean pricePerDay) {
        this.pricePerDay = pricePerDay;
    }
}